$(document).ready(function() { 
	
	jQuery.getScript(path()+'js/BelleNuitParser.js');
	jQuery.getScript(path()+'js/EdlParser.js');
	
	$('.step2').hide();
	$('.step1 .loadDefaults').click(function(e) {
		e.preventDefault();
		app.loadDefaults();
		return false;
	});
	$('.step1 .proceed').click(function(e) {
		e.preventDefault();
		app.run();
		return false;
	});
	$('.step2 .back').click(function(e) {
		e.preventDefault();
		app.back();
		return false;
	});

	$('.toggle-linenums').click(function(e) {
		e.preventDefault(); 
		$('.toggle-linenums').toggleClass('active');
		toggleLineNumbers();
		return false;
	});
	
	
	/*
	$('.step2-save').click(function(e) {
		e.preventDefault();
		var content =  escape( Utf8.encode( $('.step2 textarea').val() ) );
		jQuery.download( path()+'/download.php','name='+edl_title+'&format=srt&content=' + content );
		return false;
	});
	*/
	
	setTimeout(onresize, 100);
	$(window).resize(onresize); 
	 
	jQuery.getScript( path() + 'js/DropTarget.js', function() {
		new DropTarget( $('textarea.edl')[0] );
		new DropTarget( $('textarea.belle-nuit')[0] );
	})
});


var onresize = function() {
	$('table').each(function(i,table) {
		table = $(table);
		var table_height = $(window).height() - table.position().top;			 
		table.height( table_height );			
		table.find('.panel').height(  table_height - 10  )
		table.find('textarea').height(  table_height - 100  )
	}); 
}
var toggleLineNumbers = function() {
	 
	$('textarea.belle-nuit').each(function(a,textarea) {
		$(textarea).toggleClass('line-numbers');
		var result = [];
		var line_num = 0;
		var lines = $(textarea).val().split('\n');
		$(lines).each(function(i,line) {
			if ($(textarea).hasClass('line-numbers')) {
				if (line.substr(0,3) == '/tc') {
					line = '['+(1+line_num++)+'] ' + line;
				}				
			}
			else {
				if (line.indexOf('/tc') != -1) {
					line = '/tc'+line.split('/tc')[1];
				}
			}
			result.push(line);
		});
		$(textarea).val( result.join('\n') );
	})
};

app = {
	
	loadDefaults: function() { 
		$('textarea.edl').load(path()+'sample-files/edl.txt');
		$('textarea.belle-nuit').load(path() + 'sample-files/bellenuit.txt');
		return '...loading default files.'
	},
	
	back: function() {
		$('.step2').hide();
		$('.step1').show();	
		setTimeout(onresize, 100);
	},
	
	run: function() {
		var parser, edl_lines, bn_lines, edl_input, bn_input;
		
		edl_input = $('textarea.edl').val();
		bn_input = $('textarea.belle-nuit').val();
		
		if (!edl_input || !bn_input) {
			return alert('Please specify input for both EDL and belle Nuit files!')
		}
	  
		parser = new EdlParser();
		edl_title = parser.getTitle(edl_input);
		edl_lines = parser.parse(edl_input);
  
		parser = new BelleNuitParser();
		bn_lines = new BelleNuitParser().parse(bn_input)
		console.log('BN lines. '+bn_lines.length)
		var proceed = function() {
			$('.step1').hide();
			$('.step2').show(); 
			$('.step2 textarea').val( app.merge(edl_lines, bn_lines) );
			setTimeout(onresize, 100);
		}
		
		if (edl_lines.length == bn_lines.length) {
			proceed();
		} else if ( confirm('Line numbers do not match!\n\nEDL lines: '+edl_lines.length+'\nText lines: '+bn_lines.length+'\n\nProceed?') )  {
			proceed();
		}
		else {
			return false;
		}
	},

	merge: function(edl_lines, bn_lines) { 
		var result = ""; 
		$(edl_lines).each(function(i,line) {
			if (bn_lines[i]) {  
				result += '/tc '+line.record_in+' '+line.record_out+'\n' 
					+ bn_lines[i].text+'\n\n';
			}
		});  
		return result;
	},
	
	mergeSrt: function(edl_lines, bn_lines) { 
		var result = "";
		$(edl_lines).each(function(i,line) {
			if (bn_lines[i]) {
				var n = (i+1);
				
				var p_in = line.record_in.split(':');
				var str_in = p_in[0]+':'+p_in[1]+':'+p_in[2]+','+p_in[3]+'0';
				
				var p_out = line.record_out.split(':');
				var str_out = p_out[0]+':'+p_out[1]+':'+p_out[2]+','+p_out[3]+'0'; 
				
				var text = bn_lines[i].text;
				result += n+'\n'
					+ str_in+' --> '+str_out+'\n'
					+ text+'\n\n';
			}
		});  
		return result;
	}
	
}

